<?php
global $_GPC, $_W;
$GLOBALS['frames'] = $this->getMainMenu();
$cate = pdo_getall('snx_course',array('uniacid'=>$_W['uniacid']));
$type=empty($_GPC['type']) ? 'all' :$_GPC['type'];
$operation = !empty($_GPC['op']) ? $_GPC['op'] : 'display';
$state=$_GPC['state'];
$pageindex = max(1, intval($_GPC['page']));
$pagesize=10;
$where=' WHERE a.uniacid=:uniacid  ';
$data[':uniacid']=$_W['uniacid'];

if(isset($_GPC['keywords'])){
    // $where.="  and a.unit_name LIKE  concat('%', :name,'%')";
    $where.="  and (a.unit_name LIKE  concat('%', :name,'%') or b.name LIKE  concat('%', :name,'%') or d.cate_name LIKE  concat('%', :name,'%') or c.course_name LIKE  concat('%', :name,'%'))";
    $data[':name']=$_GPC['keywords']; 
    $type='all';  
}

 if($type =='cate'){
    $where.="and a.cour_id=".$_GPC['id'];
}

$sql="SELECT a.*,b.name,c.course_name,d.cate_name FROM ".tablename('snx_unit'). " a left join " . tablename("snx_course") . " b on a.cour_id=b.id left join" .tablename('snx_coursecate') ."c on b.c_id=c.id left join".tablename('snx_school') ."d on c.school_id=d.id".$where. " ORDER BY a.sort asc";  
$total=pdo_fetchcolumn("SELECT count(*) FROM ".tablename('snx_unit'). " a left join " . tablename("snx_course") . " b on a.cour_id=b.id left join" .tablename('snx_coursecate') ."c on b.c_id=c.id left join".tablename('snx_school') ."d on c.school_id=d.id". $where. " ORDER BY a.sort asc",$data);
$select_sql =$sql." LIMIT " .($pageindex-1) * $pagesize.",".$pagesize;
$list = pdo_fetchall($select_sql,$data);
$pager = pagination($total, $pageindex, $pagesize);
if($_GPC['op']=='delete'){
	$roe = pdo_getall("snx_task",array('unit_id'=>$_GPC['id']),array('id'));
	if (!empty($roe)) {
        	message('该课节下有课程暂时无法删除','','error');
	}else{
		$res=pdo_delete('snx_unit',array('id'=>$_GPC['id']));
	    if($res){
	        message('删除成功！', $this->createWebUrl('unit'), 'success');
	    }else{
	        message('删除失败！','','error');
	    }
	}
    
}
// if($_GPC['op']=='tg'){
// 	$res=pdo_update('snx_unit',array('state'=>2),array('id'=>$_GPC['id']));
// 	if($res){
//      message('通过成功！', $this->createWebUrl('unit'), 'success');
//     }else{
//           message('通过失败！','','error');
//     }
// }
// if($_GPC['op']=='jj'){
// 	$res=pdo_update('snx_unit',array('state'=>3),array('id'=>$_GPC['id']));
// 	if($res){
//      message('拒绝成功！', $this->createWebUrl('unit'), 'success');
//     }else{
//           message('拒绝失败！','','error');
//     }
// }
if($_GPC['op']=='change'){
	    $res = pdo_update('snx_unit',array('state'=>$_GPC['state']),array('id'=>$_GPC['id']));
		if($res){
		 message('编辑成功！', $this->createWebUrl('unit'), 'success');
		}else{
			  message('编辑失败！','','error');
		}
	}
include $this->template('web/unit');